/*
 * mysql_lib.c
 *
 *  Created on: 2011-6-26
 *      Author: vyouzhi
 */

#include <mysql.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include "mysql_lib.h"
#include "sdb_struct.h"
#include "conf_lib.h"

MYSQL  *conn;
MYSQL_ROW row;
MYSQL_FIELD *field;
unsigned int num_fields;

int connect_sql(void){
	char *mysql_host, *mysql_user, *mysql_pwd, *mysql_db, *mysql_char;

	mysql_host = conf_get("mysql_host");
	mysql_user = conf_get("mysql_user");
	mysql_pwd = conf_get("mysql_pwd");
	mysql_db = conf_get("mysql_db");
	mysql_char = conf_get("mysql_char");

	conn = mysql_init(NULL);
	if(conn == NULL)printf("error-- mysql init\n");

	mysql_options(conn, MYSQL_SET_CHARSET_NAME, "utf8");

	if (!mysql_real_connect(conn, mysql_host, mysql_user, mysql_pwd, mysql_db, 0, NULL, 0)) {		
		printf( "error mysql connect %s\n", mysql_error(conn));
		close_mysql();
		return (-1);
	}
	/*
	if (mysql_set_character_set(conn, mysql_char)){
		printf("error New client character set: %s\n", mysql_character_set_name(conn));
		free(conn);
	}*/
	if(-1 == query_mysql("set names 'utf8'")){
		printf("error character set\n");
		return (-1);
	}
	return (0);
	/*return conn;*/
}

int query_mysql(const char *sub){
	int len;
	len = strlen(sub);
	if (mysql_real_query(conn, sub, len)) {
		printf("---error: %s, sub: %s\n", mysql_error(conn), sub);
		close_mysql();
		return (-1);
	}
	return (0);
}

void commit_mysql(void){
	mysql_commit(conn);
}

long set_mysql(const char *sub){
	my_ulonglong lastid;
	
	if(-1 == query_mysql(sub)) return (-1);
	lastid = mysql_insert_id(conn);	
	commit_mysql();
	close_mysql();
	return (long)lastid;
}

char *escape_mysql(const char *sub){
	char *sqlescape;
	int len = 0;

	len = strlen(sub);
	sqlescape = (char *)calloc(len+20, sizeof(char));
	mysql_real_escape_string(conn, sqlescape, sub, len);
	return sqlescape;
}

char *simple_mysql(const char *sub){
	char *val = NULL;
	MYSQL_RES *res;

	if(-1 == mysql_query(conn, sub)) return NULL;
	res = mysql_use_result(conn);

	if (res == NULL){
		close_mysql();
		return NULL;
	}
	if((row = mysql_fetch_row(res)) != NULL){
		val = row[0];
	}
	return val;
}

Sdb *get_mysql(const char *sub, int type){
	int i , m;
	MYSQL_RES *res;

	Sdb *sdb;
	SdbRow *sdbrow;
	FieldName *h, *n, *t, *u, *r;

	/* send SQL query */
	if( -1 == query_mysql(sub)) return NULL; 
	/*mysql_query(conn, sub);*/
	res = mysql_use_result(conn);
	if (res == NULL){
		close_mysql();
		return NULL;
	}
	num_fields = mysql_num_fields(res);

	
	h = (struct FieldName *)calloc(1, sizeof(struct FieldName));
	n = h;
	for(m=0; m<num_fields; m++){
        	t = (struct FieldName *)calloc(1, sizeof(struct FieldName));
		if (type == 0){
			field = mysql_fetch_field(res);
	        	t->fn = strdup(field->name);
		}else{
			t->fn = strdup("_0");
		}
        	n->next = t;
	        n = n->next;
   	}

	mysql_field_seek(res,0);
	sdb = Sdb_init();
	while ((row = mysql_fetch_row(res)) != NULL){
		sdbrow = SdbRow_init();
		u = h;
		for(i=0; i<num_fields; i++){
			u=u->next;
			SdbRow_addColumn(sdbrow, u->fn, row[i]);
		}
		Sdb_appendRow(sdb, sdbrow);
	}
	while(h){
		r = h->next; 
		free(h->fn);
		free(h);
		h = NULL;
		h = r;
	}
	mysql_field_seek(res, 0);	
	mysql_free_result(res);

	close_mysql();	
	return sdb;
}


void close_mysql(void){
	/* close connection */
	mysql_close(conn);
	mysql_library_end();
}
